package com.cuit.qx.service.impl;

import com.cuit.qx.bean.Threshold;
import com.cuit.qx.bean.WeatherStation;
import com.cuit.qx.service.ThresholdAndStationCheckService;
import com.cuit.qx.service.ThresholdManagementService;
import com.cuit.qx.service.WeatherStationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author:wjd
 * @Description: 同步站点表和阈值表数据
 * @date:Created in 10:07 2020/10/29
 * @Modified By:
 */
@Service
public class ThresholdAndStationCheckServiceImpl implements ThresholdAndStationCheckService {

    private static final Logger LOG = LoggerFactory.getLogger(ThresholdAndStationCheckServiceImpl.class);

    @Autowired
    WeatherStationService weatherStationService;
    @Autowired
    ThresholdManagementService thresholdManagementService;

    int count = 0; //计数

    boolean isHaveStation = false;

    @Override
    public void checkThreshold() {
        LOG.info("=================Tongbu station table and threshold table start==========================");
        long startTime = System.currentTimeMillis();
        List<Threshold> byState = thresholdManagementService.list();
        List<WeatherStation> weatherStationslist = weatherStationService.getBySateWeather();

        weatherStationslist.forEach(item -> {
            byState.forEach(wimte -> {

                if (item.getStationNumber().equals(wimte.getStation())) {
                    isHaveStation = true;
                }
            });
            if (!isHaveStation) {
                Threshold threshold = new Threshold();
                threshold.setStation(item.getStationNumber());
                threshold.setStationName(item.getStationName());
                threshold.setAddress(item.getStationAddress());
                threshold.setTemperature("40");
                threshold.setPrecipitation("20");
                threshold.setHumidity("100");
                threshold.setPressure("900");
                threshold.setWind("7");
                thresholdManagementService.insertThreshold(threshold);
                count++;
            }
            isHaveStation = false;
        });
        long endTime = System.currentTimeMillis();
        LOG.info("====Tongbu station table and threshold table end time" + (endTime - startTime) + "Tongbu number：" + count + "=====");
        count = 0;
    }


}
